package MEET.src.View;

import MEET.src.code.User;
import MEET.src.code.UserDao;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class UserInfoFrame extends JFrame {
    private JPanel contentPane;

    public UserInfoFrame(User user) {
        initComponents(user);
    }

    private void initComponents(User user) {
        setTitle("上海电力大学会议预订系统 - 个人信息");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(550, 600);
        setLocationRelativeTo(null);

        // 主面板设置
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 5));
        setContentPane(contentPane);

        // 1. 顶部标题栏
        createHeaderPanel();

        // 2. 内容区
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
        mainPanel.setBackground(new Color(240, 245, 249));

        // 内部标题
        JPanel innerTitlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        innerTitlePanel.setBackground(new Color(240, 245, 249));
        JLabel innerTitle = new JLabel("个人信息详情");
        innerTitle.setFont(new Font("微软雅黑", Font.BOLD, 18));
        innerTitle.setForeground(new Color(0, 125, 153));
        innerTitlePanel.add(innerTitle);
        mainPanel.add(innerTitlePanel, BorderLayout.NORTH);

        // 信息面板
        JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new GridLayout(8, 2, 15, 15));
        infoPanel.setBorder(new EmptyBorder(20, 50, 20, 50));
        infoPanel.setBackground(Color.WHITE);

        // 修正边框设置 - 使用复合边框
        infoPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(200, 200, 200)),
                new EmptyBorder(20, 20, 20, 20)
        ));

        Font labelFont = new Font("微软雅黑", Font.BOLD, 14);
        Font valueFont = new Font("微软雅黑", Font.PLAIN, 14);

        addInfoField(infoPanel, "用户ID:", labelFont, user.getID(), valueFont);
        addInfoField(infoPanel, "姓名:", labelFont, user.getName(), valueFont);
        addInfoField(infoPanel, "年龄:", labelFont, String.valueOf(user.getAge()), valueFont);
        addInfoField(infoPanel, "性别:", labelFont, user.getSex(), valueFont);
        addInfoField(infoPanel, "区域:", labelFont, user.getUarea(), valueFont);
        addInfoField(infoPanel, "身份:", labelFont, user.getIdentity(), valueFont);
        addInfoField(infoPanel, "学院:", labelFont, user.getSdept(), valueFont);
        addInfoField(infoPanel, "权限:", labelFont, user.getUPower(), valueFont);

        mainPanel.add(infoPanel, BorderLayout.CENTER);

        // 按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setBorder(new EmptyBorder(10, 50, 20, 50));
        buttonPanel.setBackground(new Color(240, 245, 249));
        buttonPanel.setLayout(new GridLayout(1, 3, 15, 0));

        // 修改密码按钮
        JButton changePasswordBtn = createStyledButton("修改密码", new Color(70, 130, 180));
        changePasswordBtn.addActionListener(e -> changePassword(user.getID()));
        buttonPanel.add(changePasswordBtn);

        // 修改信息按钮
        JButton editInfoBtn = createStyledButton("修改信息", new Color(46, 139, 87));
        editInfoBtn.addActionListener(e -> editUserInfo(user));
        buttonPanel.add(editInfoBtn);

        // 关闭按钮
        JButton closeButton = createStyledButton("关闭", new Color(120, 120, 120));
        closeButton.addActionListener(e -> this.dispose());
        buttonPanel.add(closeButton);

        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
        contentPane.add(mainPanel, BorderLayout.CENTER);
    }

    private void createHeaderPanel() {
        JPanel headerPanel = new JPanel();
        headerPanel.setBackground(new Color(0, 125, 153));
        headerPanel.setPreferredSize(new Dimension(100, 50));

        JLabel lblTitle = new JLabel("上海电力大学会议预订系统");
        lblTitle.setForeground(Color.WHITE);
        lblTitle.setFont(new Font("微软雅黑", Font.BOLD, 20));

        headerPanel.add(lblTitle);
        contentPane.add(headerPanel, BorderLayout.NORTH);
    }

    private void addInfoField(JPanel panel, String labelText, Font labelFont, String value, Font valueFont) {
        JLabel label = new JLabel(labelText);
        label.setFont(labelFont);
        label.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(label);

        JLabel valueLabel = new JLabel(value);
        valueLabel.setFont(valueFont);
        valueLabel.setBorder(new EmptyBorder(5, 10, 5, 10));
        panel.add(valueLabel);
    }

    private JButton createStyledButton(String text, Color bgColor) {
        JButton button = new JButton(text);
        button.setFont(new Font("微软雅黑", Font.BOLD, 14));
        button.setBackground(bgColor);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(bgColor.darker(), 1),
                BorderFactory.createEmptyBorder(8, 20, 8, 20)
        ));
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));

        // 添加悬停效果
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(bgColor.brighter());
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setBackground(bgColor);
            }
        });

        return button;
    }

    private void changePassword(String userId) {
        JPasswordField newPasswordField = new JPasswordField();
        JPasswordField confirmPasswordField = new JPasswordField();

        JPanel panel = new JPanel(new GridLayout(0, 1, 10, 10));
        panel.setBorder(new EmptyBorder(20, 20, 20, 20));
        panel.add(new JLabel("新密码:"));
        panel.add(newPasswordField);
        panel.add(new JLabel("确认密码:"));
        panel.add(confirmPasswordField);

        int result = JOptionPane.showConfirmDialog(
                this, panel, "修改密码",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

        if (result == JOptionPane.OK_OPTION) {
            String newPassword = new String(newPasswordField.getPassword());
            String confirmPassword = new String(confirmPasswordField.getPassword());

            if (newPassword.isEmpty()) {
                JOptionPane.showMessageDialog(this, "密码不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (!newPassword.equals(confirmPassword)) {
                JOptionPane.showMessageDialog(this, "两次输入的密码不一致", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (UserDao.updatePassword(userId, newPassword)) {
                JOptionPane.showMessageDialog(this, "密码修改成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "密码修改失败", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void editUserInfo(User user) {
        new EditUserInfoFrame(user).setVisible(true);
        this.dispose();
    }
}